import { ref ,computed, onUnmounted} from 'vue'
import dayjs from 'dayjs'
export const useCountDown = () => {
    let timer = null
    // 初始化的时间，默认值为0
    const fromTime = ref(0)
    // (2)格式化时间
    // const newTime = computed(() => dayjs.unix(fromTime.value).format('mm分ss秒'))
    const newTime = computed(() => dayjs.unix(fromTime.value).format('mm分ss秒'))
    // 开启倒计时   currentTime:传过来的秒数
    const start = (currentTime) => {
        // 开启倒计时逻辑
        // (1)将传过来的秒数赋值给fromTime，然后开启倒计时
        fromTime.value = currentTime
      timer = setInterval(() => {
            fromTime.value --
        },1000)
    }
    // 组件销毁时，清除定时器
    onUnmounted(()=>{
        timer&&clearInterval(timer)
    })
    return{
        fromTime,start,newTime
    }
}